package com.example.chapp.control;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
;
import com.example.chapp.MainActivity;
import com.example.chapp.R;
import com.example.chapp.entity.result;
import com.example.chapp.util.HttpCallback;
import com.example.chapp.util.httputil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;

public class redegitactive extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.redegit);
        View but11=findViewById(R.id.but_2);
        but11.setOnClickListener(new View.OnClickListener() {
            Handler mHandler =new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(@NonNull Message message) {
                    Gson gson=new Gson();
                    System.out.println(message);
                    if (message.what==1){
                        result r = gson.fromJson(message.obj.toString(),new TypeToken<result<Boolean>>(){}.getType());
                        if ((boolean)r.getData()){
                            Toast toast = Toast.makeText(redegitactive.this,"注册成功!!",Toast.LENGTH_LONG);
                            toast.setGravity(Gravity.CENTER,0,0);
                            toast.show();
                            Intent intent=new Intent(redegitactive.this, loginactive.class);
                            startActivity(intent);
                        }
                        else {
                            Toast toast = Toast.makeText(redegitactive.this,"注册失败!!",Toast.LENGTH_LONG);
                            toast.setGravity(Gravity.CENTER,0,0);
                            toast.show();
                        }
                    }
                    else {
                        Toast toast = Toast.makeText(redegitactive.this,"注册失败!!",Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER,0,0);
                        toast.show();
                    }
                    return false;
                }
            });
            @Override
            public void onClick(View view) {
                TextView n=findViewById(R.id.admin1);
                TextView p=findViewById(R.id.pwd1);
                httputil.sendRequest("http://192.168.201.1:8080/adduser/"+n.getText()+"/"+p.getText(),
                        "GET",
                        "page=0&&count=25",
                        new HttpCallback() {

                            @Override
                            public void onSuccess(InputStream in) {
                                String result = httputil.decodeAsString(in);
                                Message msg = new Message();
                                msg.what = 1;
                                msg.obj = result;
                                mHandler.sendMessage(msg);
                            }

                            @Override
                            public void onFailure(int code, String mes) {
                                Message msg = new Message();
                                msg.what = 2;
                                msg.obj = mes;
                                mHandler.sendMessage(msg);
                            }

                        });


            }
        });
        View b = findViewById(R.id.za);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent=new Intent(redegitactive.this, loginactive.class);
                startActivity(intent);
            }
        });


    }
}
